package dm5

import (
	"fmt"
	"git.oschina.net/gladmo/manga/spider"
	xmlpath "gopkg.in/xmlpath.v2"
	"io"
	// "git.oschina.net/gladmo/manga/tools"
	"net/http"
	"strings"
)

var host string = "http://www.dm5.com"

var baseUrl string = "http://www.dm5.com/manhua-list-size60/"

//封面存在地址
var coverPath string = "manga/dm5/cover/"

//漫画存在地址
var mangaPath string = "manga/dm5/manga/"

var site string = "DM5"

func Dm5() {
	client := &http.Client{}

	//获取爬取页信息
	req, _ := http.NewRequest("GET", baseUrl, nil)
	req.Header.Add("Accept-Language", "zh-CN,zh;q=0.8,en;q=0.6")
	req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36")
	mangaList, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer mangaList.Body.Close()

	//获取漫画列表
	mList := getMangas(mangaList.Body)
	fmt.Println(mList)
	// getMangaInfo(mList)
}

//从首页获取漫画链接
func getMangas(body io.ReadCloser) map[string]spider.MangaList {
	node, err := xmlpath.ParseHTML(body)

	//建立一个[code]{name,url}的map
	mangaList := make(map[string]spider.MangaList)

	if err != nil {
		fmt.Println(err)
	}

	//获取所有<dt><a>节点
	liPath := xmlpath.MustCompile("//*[@id='index_left']/div/div[@class='innr3']/li/a[1]")

	it := liPath.Iter(node)
	for it.Next() {
		titlePath := xmlpath.MustCompile("strong/text()")
		title, _ := titlePath.String(it.Node())

		urlPath := xmlpath.MustCompile("@href")
		url, _ := urlPath.String(it.Node())

		code := strings.Trim(strings.Replace(url, "manhua-", "", 1), "/")
		mangaList[code] = spider.MangaList{title, host + url}
	}
	return mangaList
}

// func getMangaInfo(list map[string]spider.MangaList) {
// 	for k, v := range list {
// 		//请求之前先判断是否存在些漫画
// 		if !spider.HasManga(k) {
// 			client := &http.Client{}
// 			req, _ := http.NewRequest("GET", v.Url, nil)
// 			req.Header.Add("Accept-Language", "zh-CN,zh;q=0.8,en;q=0.6")
// 			req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36")
// 			info, err := client.Do(req)
// 			if err != nil {
// 				fmt.Println(err)
// 				return
// 			}
// 			saveMangaInfo(info.Body, k, v.Name)
// 			info.Body.Close()
// 		}

// 		info, _ := http.Get(v.url)
// 		defer info.Body.Close()
// 		fmt.Println("get chapters")
// 		//如果漫画已经存在，下载章节
// 		urls, titles := getChapters(info.Body)
// 		for key, value := range urls {
// 			titleArr := strings.Split(titles[key], "-")
// 			no := strings.TrimSpace(titleArr[len(titleArr)-1])

// 			fNo := tools.FormatNo(no)

// 			//章节是否存在
// 			manga_id := model.FindManga(k)
// 			if !spider.HasChapter(manga_id, fNo) {
// 				pageNo, ok := crawl(value, k, tools.ParseNo(fNo))
// 				if ok {
// 					//保存章节信息
// 					chapter := model.Chapter{
// 						Manga_id:    manga_id,
// 						Title:       titles[key],
// 						No:          fNo,
// 						Total_pages: pageNo,
// 						Entry_time:  time.Now().Unix(),
// 					}
// 					c, _ := model.AddChapter(chapter)

// 					//更新manga章节信息等
// 					model.UpdateManga(manga_id)

// 					//保存爬取记录
// 					log := model.Log{
// 						Manga_id:   manga_id,
// 						Chapter_id: c.Chapter_id,
// 						Site:       site,
// 						Code:       k,
// 						Title:      titles[key],
// 						Url:        value,
// 					}
// 					model.AddLog(log)
// 				}
// 			}
// 		}
// 	}
// }

// func saveMangaInfo(body io.Reader, code string, title string) bool {

// 	node, err := xmlpath.ParseHTML(body)
// 	if err != nil {
// 		fmt.Println(err)
// 	}

// 	baseInfo := xmlpath.MustCompile("//dl[@class='dl-horizontal']")

// 	info := baseInfo.Iter(node)
// 	infoMap := map[string]string{
// 		"1": "altname",
// 		"2": "status",
// 		"3": "categories",
// 		"4": "language",
// 		"5": "author",
// 		"6": "artist",
// 	}

// 	//获取基本信息，加入到 dataMap 中
// 	dataMap := make(map[string]string)
// 	if info.Next() {
// 		for k, v := range infoMap {
// 			dd := xmlpath.MustCompile("dd[" + k + "]")
// 			value, ok := dd.String(info.Node())
// 			if !ok {
// 				value = ""
// 			}
// 			if value != "" && k == "3" {
// 				re := regexp.MustCompile(`\s+`)
// 				value = re.ReplaceAllString(value, " ")
// 			}
// 			dataMap[v] = strings.TrimSpace(value)
// 		}
// 	}
// 	//获取封面地址
// 	cPath := xmlpath.MustCompile("//div[@class='col-md-4']/img/@src")
// 	cover, ok := cPath.String(node)
// 	if !ok {
// 		cover = ""
// 	}
// 	dataMap["cover"] = cover

// 	//获取漫画描述
// 	summaryPath := xmlpath.MustCompile("//div[@class='note note-default margin-top-15']/p/text()")
// 	summary, ok := summaryPath.String(node)
// 	if !ok {
// 		summary = ""
// 	}

// 	dataMap["summary"] = summary

// 	//下载漫画封面
// 	fileName, err := tools.GetCover(coverPath, dataMap["cover"])
// 	if err != nil {
// 		fmt.Println(err)
// 	}
// 	dataMap["cover"] = fileName

// 	//保存数据库
// 	manga := model.Manga{
// 		Title:      title,
// 		Code:       code,
// 		Altname:    dataMap["altname"],
// 		Cover:      dataMap["cover"],
// 		Summary:    dataMap["summary"],
// 		Status:     dataMap["status"],
// 		Language:   dataMap["language"],
// 		Author:     dataMap["author"],
// 		Artist:     dataMap["artist"],
// 		Categories: dataMap["categories"],
// 	}
// 	return model.AddManga(manga)
// }
